
package kaizone.haining.chaobohui.widget;

import kaizone.haining.chaobohui.CBHApplication;
import kaizone.haining.chaobohui.R;
import kaizone.haining.chaobohui.setup.Shareter;
import kaizone.haining.chaobohui.setup.Shareter.ShareEntry;
import kaizone.haining.chaobohui.setup.WeiXin;
import kaizone.haining.chaobohui.setup.XinLangWeiBo;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.Toast;

public class ShareDialog extends Dialog implements android.view.View.OnClickListener {

    private ImageButton mButton01;

    private ImageButton mButton02;

    private Object mShareObject;
    
    private CheckBox mCbTimeline;

    public ShareDialog(Context context, Object shareObj) {
        super(context, android.R.style.Theme_Dialog);
        mShareObject = shareObj;
        if (context instanceof Activity) {
            setOwnerActivity((Activity) context);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle("分享至");
        setContentView(R.layout.share_dialog);
        mButton01 = (ImageButton) findViewById(R.id.img01);
        mButton02 = (ImageButton) findViewById(R.id.img02);
        mButton01.setOnClickListener(this);
        mButton02.setOnClickListener(this);
        mCbTimeline = (CheckBox) findViewById(R.id.checkbox_1);
    }

    @Override
    public void onClick(View v) {
        if (v == mButton01) {
            bindXinLangWeibo();
        }//
        else if (v == mButton02) {
            bindWeiXin();
        }
        dismiss();
    }

    public void bindXinLangWeibo() {
        if (getOwnerActivity() instanceof Activity) {
            if (mShareObject instanceof ShareEntry) {
                CBHApplication app = (CBHApplication) (getOwnerActivity()).getApplication();
                XinLangWeiBo weibo = app.getXinLangWeiBo();
                if (weibo == null) {
                    weibo = new XinLangWeiBo(app);
                    app.setXinLangWeibo(weibo);
                    // Toast.makeText(getContext(), "请微博绑定", 500).show();
                    return;
                }
                if (!weibo.isSessionValid()) {
                    weibo.authorize(getContext());
                    return;
                }
                final ShareEntry entry = (ShareEntry) mShareObject;
                final String shareStr = Shareter.editShareText(entry);
                if (TextUtils.isEmpty(shareStr))
                    return;
                weibo.update(shareStr, "", "");
            }
        }
    }

    public void bindWeiXin() {
        if (mShareObject == null)
            return;
        CBHApplication app = (CBHApplication) (getOwnerActivity()).getApplication();
        WeiXin weiXin = app.getWeiXin();
        if (weiXin == null) {
            weiXin = new WeiXin(getContext());
            app.setWeiXin(weiXin);
        }
        final boolean scence = mCbTimeline.isChecked();
        final ShareEntry entry = (ShareEntry) mShareObject;
        final String shareStr = Shareter.editShareText(entry);
        if (TextUtils.isEmpty(shareStr))
            return;
        weiXin.upload(shareStr, scence);
        // weiXin.upload(entry.status, entry.imageUrl);
    }

}
